﻿using System;
using System.Windows.Forms;
using 售后客户管理.Common;
using 售后客户管理.ModelLibrary;
using 售后客户管理.SqlClassLibrary;


namespace 售后客户管理.Forms
{
    public partial class FmCustomerInfo : Form
    {
        public FmCustomerInfo()
        {
            InitializeComponent();
        }
        public FmCustomerInfo(Customer customer)
        {
            InitializeComponent();
            ObjectToApp(customer);
        }
        private SqlConnectionHelper SqlConnectionHelper = new SqlConnectionHelper(Program.configs,ConfigCode.Main);
        private void FmCustomerInfo_Load(object sender, EventArgs e)
        {
            this.dataGridView1.BackgroundColor = MyColor.NewColor_Lavender;
            this.btnSave.BackColor = MyColor.NewColor_Lavender;
            this.btnCancel.BackColor = MyColor.NewColor_Lavender;
            this.btnSave.Paint += DrawingImage.RoundButton_Paint;
            this.btnCancel.Paint += DrawingImage.RoundButton_Paint;
            this.cbbssShangwu.DataSource = SqlGetSet.GetDictList("shangwu", SqlConnectionHelper.sqlConnection);
            this.cbbmsVersions.Items.AddRange(SqlGetSet.GetDictList("version", SqlConnectionHelper.sqlConnection).ToArray());
            this.cbbmShishi.Items.AddRange(SqlGetSet.GetDictList("shishi", SqlConnectionHelper.sqlConnection).ToArray());
            this.dataGridView1.DoubleBuffered(true);
            this.dataGridView1.DoubleClick += DataGridView1_DoubleClick;
            this.fbcCustbh.Focus();
        }

        private void DataGridView1_DoubleClick(object sender, EventArgs e)
        {
            Hetong hetong = new Hetong();
            hetong = DrawingImage.DataGridViewSelectedRowToObject<Hetong>(this.dataGridView1, hetong);
            FmHetong fmHetong = new FmHetong(AppToObject(), hetong, SqlConnectionHelper.sqlConnection);
            fmHetong.ShowDialog();

            this.dataGridView1.DataSource = SqlGetSet.GetHetongDT(new Hetong() { Custid = this.tbCustid.Text }, SqlConnectionHelper.sqlConnection);
            DrawingImage.ReNameDataGridView(this.dataGridView1, SqlConnectionHelper.sqlConnection);
        }
        private void BtnSave_Click(object sender, EventArgs e)
        {
            if (this.fbcCustname.Text == "")
            {
                return;
            }
            if (SqlGetSet.SetCustomer(AppToObject(), SqlConnectionHelper.sqlConnection) > 0)
            {
                if (MessageBox.Show("保存成功！是否清除界面信息？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                    ObjectToApp(new Customer());
                this.fbcCustbh.Focus();
                return;
            }
            MessageBox.Show("保存失败！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        private void BtnCancel_Click(object sender, EventArgs e)
        {

            ObjectToApp(new Customer());
            this.fbcCustbh.Focus();
        }
        private void FbcCustbh_DoubleClick(object sender, System.EventArgs e)
        {
            if (this.fbcCustbh.Text == "")
            {
                this.fbcCustbh.Text = SqlGetSet.GetBh("customer", SqlConnectionHelper.sqlConnection);
            }
            else
            {
                FmSelectMenu fmSelectMenu = new FmSelectMenu("customer", new Customer()
                {
                    Custbh = this.fbcCustbh.Text,
                    Custname = this.fbcCustbh.Text,
                    Pym = this.fbcCustbh.Text,
                }, SqlConnectionHelper.sqlConnection);
                fmSelectMenu.Selected += FmSelectMenu_Selected;
                fmSelectMenu.ShowDialog();

            }
        }
        private void FbcCustbh_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (this.fbcCustbh.Text == "")
                {
                    this.fbcCustbh.Text = SqlGetSet.GetBh("customer", SqlConnectionHelper.sqlConnection);
                }
                else
                {
                    FmSelectMenu fmSelectMenu = new FmSelectMenu("customer", new Customer()
                    {
                        Custbh = this.fbcCustbh.Text,
                        Custname = this.fbcCustbh.Text,
                        Pym = this.fbcCustbh.Text,
                    }, SqlConnectionHelper.sqlConnection);
                    fmSelectMenu.Selected += FmSelectMenu_Selected;
                    fmSelectMenu.ShowDialog();

                }
            }
        }
        /// <summary>
        /// The form's HotKey Ctrl+S;
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FmCustomerInfo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.S && e.Control)
            {
                BtnSave_Click(this.btnSave, new EventArgs());
            }
        }
        private void 新增ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.tbCustid.Text == "")
            {
                return;
            }
            FmHetong fmHetong = new FmHetong(AppToObject(), null, SqlConnectionHelper.sqlConnection);
            fmHetong.ShowDialog();

            this.dataGridView1.DataSource = SqlGetSet.GetHetongDT(new Hetong() { Custid = this.tbCustid.Text }, SqlConnectionHelper.sqlConnection);
            DrawingImage.ReNameDataGridView(this.dataGridView1, SqlConnectionHelper.sqlConnection);
        }
        private void FmSelectMenu_Selected(object customers)
        {
            ObjectToApp((Customer)customers);
        }


        public void ObjectToApp(Customer customer)
        {
            this.cbBeactive.Checked = customer.Beactive == "Y" ? true : false;
            this.fbcBeizhu.Text = customer.Beizhu;
            this.fbcCustAdd.Text = customer.CustAdd;
            this.fbcCustbh.Text = customer.Custbh;
            this.fbcCustname.Text = customer.Custname;
            this.fbcDonglenum.Text = customer.Donglenum;
            this.fbcLxdh.Text = customer.Lxdh;
            this.fbcLxr.Text = customer.Lxr;
            this.fbcSitenum.Text = customer.Sitenum;
            this.cbbssShangwu.Text = customer.Shangwu;
            this.cbbmsVersions.Text = customer.Version;
            this.tbCustid.Text = customer.Custid;
            this.cbbmShishi.Text = customer.Shishi;
            this.fbcQuyu.Text = customer.Quyu;

            this.dataGridView1.DataSource = SqlGetSet.GetHetongDT(new Hetong() { Custid = customer.Custid }, SqlConnectionHelper.sqlConnection);
            DrawingImage.ReNameDataGridView(this.dataGridView1, SqlConnectionHelper.sqlConnection);
            this.fbcCustbh.Focus();
        }
        private Customer AppToObject()
        {
            Customer customer = new Customer();
            customer.Beactive = this.cbBeactive.Checked ? "Y" : "N";
            customer.Beizhu = this.fbcBeizhu.Text;
            customer.CustAdd = this.fbcCustAdd.Text;
            customer.Custbh = this.fbcCustbh.Text;
            customer.Custname = this.fbcCustname.Text;
            customer.Donglenum = this.fbcDonglenum.Text;
            customer.Lxdh = this.fbcLxdh.Text;
            customer.Lxr = this.fbcLxr.Text;
            customer.Sitenum = this.fbcSitenum.Text;
            customer.Shangwu = this.cbbssShangwu.Text;
            customer.Version = this.cbbmsVersions.Text;
            customer.Custid = this.tbCustid.Text;
            customer.Shishi = this.cbbmShishi.Text;
            customer.Quyu = this.fbcQuyu.Text;
            return customer;
        }

    }
}
